import 'package:rainflutter/Tools/dcp_pch.dart';
import 'package:flutter/material.dart';
import 'package:flutter_easyrefresh/easy_refresh.dart';
import 'package:flutter_easyrefresh/material_footer.dart';
import 'package:flutter_easyrefresh/material_header.dart';

class ListviewRefresh extends StatefulWidget {
  @override
  _ListviewRefreshState createState() => _ListviewRefreshState();
}

class _ListviewRefreshState extends State<ListviewRefresh>
    with SingleTickerProviderStateMixin {
  EasyRefreshController _controller;
  List<String> addStrList = [
    "11",
    "22",
    "33",
    "44",
    "55",
    "66",
    "77",
    "88",
    "99",
    "00"
  ];
  List<String> strList = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "0"];

  @override
  void initState() {
    super.initState();
    _controller = EasyRefreshController();
  }

  @override
  void dispose() {
    super.dispose();
    _controller.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: Text('listview'),
          centerTitle: true,
        ),
        body: new EasyRefresh(
          header: MaterialHeader(
            valueColor: new AlwaysStoppedAnimation(Color(0xffff3b00)),
          ),
          footer: MaterialFooter(
            displacement: 120 + MediaQuery.of(context).padding.bottom,
            valueColor: new AlwaysStoppedAnimation(Color(0xffff3b00)),
          ),
          child: ListView.builder(
            itemCount: strList.length,
            itemBuilder: (context, index) {
              return GestureDetector(
                onTap: () {
                  print(index.toString() + '====' + strList[index]);
                  showToast(strList[index]);
                },
                child: ExpansionTile(
                  title: Text("服务"),
                  children: [
                    Container(
                        height: 70.0,
                        child: Card(
                          child: new Center(
                            child: new Text(
                              strList[index],
                              style: new TextStyle(fontSize: 18.0),
                            ),
                          ),
                        )),Container(
                        height: 70.0,
                        child: Card(
                          child: new Center(
                            child: new Text(
                              strList[index],
                              style: new TextStyle(fontSize: 18.0),
                            ),
                          ),
                        )),Container(
                        height: 70.0,
                        child: Card(
                          child: new Center(
                            child: new Text(
                              strList[index],
                              style: new TextStyle(fontSize: 18.0),
                            ),
                          ),
                        )),
                  ],
                ),
              );
            },
          ),
          onRefresh: () async {
            await new Future.delayed(const Duration(seconds: 1), () {
              setState(() {
                strList.clear();
                // strList.addAll(addStrList);
                strList = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "0"];
                print(strList);
              });
            });
          },
          onLoad: strList.length == 20
              ? null
              : () async {
                  await new Future.delayed(const Duration(seconds: 1), () {
                    if (strList.length < 20) {
                      setState(() {
                        strList.addAll(addStrList);
                        print(strList);
                      });
                    } else {
                      print('object---');
                      // _controller.finishRefresh(success: true);
                      _controller.finishLoad(success: true, noMore: false);
                    }
                  });
                },
        ));
  }
}
